/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.awt.Component;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;
import stamboom.Geslacht;

/**
 *
 * @author Cabez
 */
public class PersoonPanel extends javax.swing.JPanel {

    /**
     * Creates new form PersoonPanel
     */
    public PersoonPanel() {
        initComponents();
    }

    public void setAchternaam(String achternaam) {
        tfAchternaam.setText(achternaam);
    }

    public void setPersoonsnummer(int nr) {
        tfNummer.setText("" + nr);
    }

    public void setVoornamen(String voornamen) {
        tfVoornamen.setText(voornamen);
    }

    public void setTussenvoegsel(String tussenvoegsel) {
        tfTussenvoegsel.setText(tussenvoegsel);
    }

    public void setGebDat(String datum) {
        tfGeboortedatum.setText(datum);
    }

    public void setGeslacht(Geslacht g) {
        if (g == Geslacht.MAN) {
            radioM.setSelected(true);
        } else if (g == Geslacht.VROUW) {
            radioV.setSelected(true);
        }
    }

    public Geslacht getGeslacht() {
        if (radioM.isSelected()) {
            return Geslacht.MAN;
        } else {
            return Geslacht.VROUW;
        }
    }

    public String[] getVoornamen() {
        if (!tfVoornamen.getText().isEmpty()) {
            return tfVoornamen.getText().trim().split(" ");
        } else {
            return null;
        }
    }

    public String getTussenvoegsel() {
        return tfTussenvoegsel.getText();
    }

    public String getAchternaam() {
        if (tfAchternaam.getText().isEmpty()) {
            return null;
        } else {
            return tfAchternaam.getText();
        }
    }

    public GregorianCalendar getGebDat() {
        return this.datum(this, tfGeboortedatum.getText());
    }

    static GregorianCalendar datum(Component parent, String datumaanduiding) {
        try {
            String[] datumSplit = datumaanduiding.split("-");
            if (datumSplit.length != 3) {
                JOptionPane.showMessageDialog(parent, "Ongeldige datum", "",
                        JOptionPane.ERROR_MESSAGE);
                return null;
            }
            int dag = Integer.parseInt(datumSplit[0]);
            if (dag <= 0 || dag > 31) {
                JOptionPane.showMessageDialog(parent,
                        "Ongeldige dag", "",
                        JOptionPane.ERROR_MESSAGE);
                return null;
            }

            int maand = Integer.parseInt(datumSplit[1]);
            if (maand <= 0 || maand > 12) {
                JOptionPane.showMessageDialog(parent,
                        "Ongeldige maand", "",
                        JOptionPane.ERROR_MESSAGE);
                return null;
            }

            int jaar = Integer.parseInt(datumSplit[2]);

            // maanden zijn binnen GregorianCalendar van 0 t/m 11 gecodeerd:
            return new GregorianCalendar(jaar, maand - 1, dag);
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(parent, "Ongeldige datum", "",
                    JOptionPane.ERROR_MESSAGE);
            return null;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        tfNummer = new javax.swing.JTextField();
        tfAchternaam = new javax.swing.JTextField();
        tfVoornamen = new javax.swing.JTextField();
        tfTussenvoegsel = new javax.swing.JTextField();
        radioM = new javax.swing.JRadioButton();
        radioV = new javax.swing.JRadioButton();
        jLabel5 = new javax.swing.JLabel();
        tfGeboortedatum = new javax.swing.JTextField();

        jLabel1.setText("Nummer");

        jLabel2.setText("Achternaam");

        jLabel3.setText("Voornamen");

        jLabel4.setText("Tussenvoegsel");

        buttonGroup.add(radioM);
        radioM.setText("mannelijk");
        radioM.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioMActionPerformed(evt);
            }
        });

        buttonGroup.add(radioV);
        radioV.setText("vrouwelijk");
        radioV.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioVActionPerformed(evt);
            }
        });

        jLabel5.setText("Geb datum (dd-mm-jjjj)");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(8, 8, 8)
                        .addComponent(tfGeboortedatum))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(61, 61, 61)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tfAchternaam)
                            .addComponent(tfNummer)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(radioM))
                        .addGap(48, 48, 48)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(radioV)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(tfVoornamen, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)
                            .addComponent(tfTussenvoegsel))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfNummer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfAchternaam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfVoornamen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfTussenvoegsel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(radioM)
                    .addComponent(radioV))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(tfGeboortedatum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void radioMActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioMActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_radioMActionPerformed

    private void radioVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioVActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_radioVActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JRadioButton radioM;
    private javax.swing.JRadioButton radioV;
    private javax.swing.JTextField tfAchternaam;
    private javax.swing.JTextField tfGeboortedatum;
    private javax.swing.JTextField tfNummer;
    private javax.swing.JTextField tfTussenvoegsel;
    private javax.swing.JTextField tfVoornamen;
    // End of variables declaration//GEN-END:variables
}
